﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace FeedNet
{
    public class Entry
    {
        private string _title;
        private string _url;
        private string _author;
        private string _description;
        private string _content;
        private string _guid;
        private DateTime _date_published;

        public string title
        {
            get{return _title;}
            set{_title = value; }
        }

        public string url
        {
            get { return _url; }
            set { _url = value; }
        }

        public string author
        {
            get { return _author; }
            set { _author = value; }
        }

        public string description
        {
            get { return _description; }
            set { _description = value; }
        }

        public string content
        {
            get { return _content; }
            set { _content = value; }
        }

        public string guid
        {
            get { return _guid; }
            set { _guid = value; }
        }

        public DateTime date_published
        {
            get { return _date_published; }
            set { _date_published = value; }
        }


        /*
         * Create an Entry object from an XmlTextReader that is at the Entry position
         * 
         */
        public static Entry readEntry(XmlTextReader reader)
        {
            Entry myEntry = new Entry();
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.EndElement && (reader.Name == "item" || reader.Name == "entry"))
                {
                    return myEntry;
                }

                switch (reader.Name)
                {
                    case "title":
                        myEntry.title = reader.ReadString();
                        break;
                    case "link":
                        if (reader.AttributeCount > 0)
                        {
                            myEntry.url = reader.GetAttribute("href");
                            int q = 0;
                        }
                        else
                        {
                            myEntry.url = reader.ReadString();
                        }
                        break;
                    case "dc:creator":
                        myEntry.author = reader.ReadString();
                        break;
                    case "summary":
                    case "description":
                        myEntry.description = reader.ReadString();
                        break;
                    case "content":
                    case "content:encoded":
                        myEntry.content = reader.ReadString();
                        break;
                    case "id":
                    case "guid":
                        myEntry.guid = reader.ReadString();
                        break;
                    case "published":
                    case "pubDate":
                        myEntry.date_published = DateTime.Parse(reader.ReadString());
                        break;
                }
            }

            return null;
        }

        public Entry()
        {
        }

    }
}
